package com.example.myapp.Activity;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.myapp.ADO.ActivityManager;
import com.example.myapp.ADO.MyDialog;
import com.example.myapp.Model.User;
import com.example.myapp.R;

import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.BmobSMS;
import cn.bmob.v3.BmobUser;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;
import cn.bmob.v3.listener.QueryListener;
import cn.bmob.v3.listener.UpdateListener;

public class GetPwd extends AppCompatActivity {
   private Button setcode;
   private int p=60;
   private   Timer timer;
    //开启异步线程
    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);

            switch (msg.what) {

                case 0: {
                    p--;
                    setcode.setText("重新发送("+p+"秒)");
                    setcode.setEnabled(false);
                    if (p==0){
                        setcode.setEnabled(true);
                        setcode.setText("获取验证码");
                        timer.cancel();
                    }
                    Log.w("aaaa", "发送验证码成功，");

                }break;
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        ActivityManager.getInstance().addActivity(this);
        super.onCreate(savedInstanceState);
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);  //隐藏标题栏
        setContentView(R.layout.getpwd);
        ImageView getpwd_back=findViewById(R.id.getpwd_back);
        EditText getpwdphone=findViewById(R.id.getpwdphone);
        EditText code=findViewById(R.id.code);
        EditText getnewpwd1 =findViewById(R.id.getnewpwd1);
        EditText getnewpwd2=findViewById(R.id.getnewpwd2);
        TextView getpwd_ok=findViewById(R.id.getpwd_ok);
        setcode=findViewById(R.id.setcode);









        setcode.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isMobileNO(getpwdphone.getText().toString())) {

                    BmobQuery<User> categoryBmobQuery = new BmobQuery<>();
                    categoryBmobQuery.addWhereEqualTo("mobilePhoneNumber",getpwdphone.getText().toString());
                    categoryBmobQuery.findObjects(new FindListener<User>() {
                        @Override
                        public void done(List<User> object, BmobException e) {
                            if (e == null) {
                                if (object.size()>0){
                                    Log.w("aaaa", "手机有效" + object);

                                    // TODO 设置定时器
                                    timer = new Timer();
                                    timer.schedule(new TimerTask() {
                                        @Override
                                        public void run() {
                                            Message message = new Message();
                                            message.what = 0;
                                            handler.sendMessage(message);
                                        }
                                    }, 1000, 2000);  // TODO 每隔一秒执行一下

                                    BmobSMS.requestSMSCode(getpwdphone.getText().toString(), "", new QueryListener<Integer>() {
                                        @Override
                                        public void done(Integer smsId, BmobException e) {
                                            if (e == null) {
                                                Log.w("aaaa", "发送验证码成功，短信ID：" + smsId + "\n");
                                                Toast.makeText(GetPwd.this, "发送验证码成功", Toast.LENGTH_LONG).show();
                                            } else {
                                                Log.w("aaaa", "发送验证码失败：" + e.getErrorCode() + "-" + e.getMessage() + "\n");
                                                Toast.makeText(GetPwd.this, "发送验证码失败", Toast.LENGTH_LONG).show();
                                            }
                                        }
                                    });

                                }else {
                                    Toast.makeText(GetPwd.this, "该手机号未注册，请重试！", Toast.LENGTH_LONG).show();

                                }
                            } else {
                                Log.w("aaaa", "系统出错");
                            }
                        }
                    });


                }else {
                    Toast.makeText(GetPwd.this, "手机格式错误", Toast.LENGTH_LONG).show();
                }
            }
        });
        getpwd_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        getpwd_ok.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (getnewpwd1.getText().toString().trim().length() < 6) {
                    Toast.makeText(GetPwd.this, "密码不能少于6位数", Toast.LENGTH_LONG).show();
                } else {
                    if (!getnewpwd1.getText().toString().equals(getnewpwd2.getText().toString())) {
                        Toast.makeText(GetPwd.this, "两次密码不一致，请重试", Toast.LENGTH_LONG).show();
                    } else {

                        BmobUser.resetPasswordBySMSCode(code.getText().toString(), getnewpwd1.getText().toString(), new UpdateListener() {
                            @Override
                            public void done(BmobException e) {
                                if (e == null) {
                                    Log.w("aaaa","重置成功");
                                    Toast.makeText(GetPwd.this, "重置成功", Toast.LENGTH_LONG).show();
                                    finish();
                                } else {
                                    Log.w("aaaa","重置失败：" + e.getErrorCode() + "-" + e.getMessage());
                                    Toast.makeText(GetPwd.this, "验证码错误", Toast.LENGTH_LONG).show();
                                }
                            }
                        });

                    }
                }

            }
        });






    }
    public void onBackPressed() {
        //此处写退向后台的处理
        finish();
    }//手机返回键
    //正则表达验证手机号
    public static boolean isMobileNO(String mobiles){
        String telRegex = "[1][358]\\d{9}";
        if (TextUtils.isEmpty(mobiles)){
            return false;
        }
        else return mobiles.matches(telRegex);
    }
}